﻿@model TWCRM.Models.Home.AccountModel
@{
    Layout = "/Views/Shared/_SubLayout.cshtml";
    ViewBag.Title = "帳戶";
}
<script>
    var isNew = true;
    $(document).ready(function () {
        isNew = $.url().param('id') == undefined;

        if (isNew)
            $('.metro_input_contain input:first').focus();

        $.fn.metro.onPrevClick = function () {
            window.history.go(-1);
        };

        adjustMetroInputContain();

        loadAuthoritys();
    });
</script>
<script id="authorityCheckTemplate" type="text/x-jquery-tmpl">
    <label>
        <input type="checkbox" class="authority" authority="${Authority}"/>
        ${Authority}
    </label>
</script>
<div id="AccountEdit">
    @using (Html.BeginForm())
    {
        @Html.HiddenFor(m => m.ID)
        @Html.HiddenFor(m => m.IsNew)
        <div class="metro_toolbar">
            <div class="metro_input_contain">
                <a class="metro_input_btn" onclick="confirm()">儲存帳戶</a>
            </div>
        </div>
        
        <div class="metro_input_contain">
            <label for="Name" class="metro_input_caption">
                使用者名稱
            </label>
            @Html.TextBoxFor(m => m.Name, new { @class = "metro_input_textinput one_line" })
        </div>
        <div class="metro_input_contain">
            <label for="Password" class="metro_input_caption">
                密碼
            </label>
            @Html.TextBoxFor(m => m.Password, new { @class = "metro_input_textinput one_line" })
        </div>
        <div class="metro_input_contain">
            <label for="Authority" class="metro_input_caption">
                權限
            </label>
            <div class="metro_option_group one_line">
                
            </div>
            @Html.HiddenFor(m => m.Authority, new { @class = "txt_Authority" })
        </div>
    }
</div>
<script>
    function confirm() {
        if (!valEmpty($("#Name"))) {
            showError("使用者名稱是必要欄位不可留白");
            return;
        }
        if (!valEmpty($("#Password"))) {
            showError("密碼是必要欄位不可留白");
            return;
        }

        var authority = '';
        $('.authority').each(function () {
            var authorityName = $(this).attr('authority');
            var allow = $(this).attr('checked');
            authority += authorityName + "●" + allow + "◆";
        });
        $('.txt_Authority').val(authority);

        $('form').submit();
    }

    function adjustMetroInputContain() {
        var maxCaptionWidth = $.fn.calcMaxWidth($('.metro_input_caption'));
        $('.metro_input_caption').each(function () {
            $(this).width(maxCaptionWidth);
        });
    }

    function loadAuthoritys() {
        showLoading();
        var url = "/Home/GetAuthoritys";
        $.ajax({
            url: url,         //请求链接 
            dataType: "json",
            success: function (result) {
                $('#authorityCheckTemplate').tmpl(result).appendTo($('.metro_option_group'));

                if (!isNew) {
                    var $authorityList = $('item', $($('#authorityList').html()));
                    $authorityList.each(function () {
                        $this = $(this);
                        if ($this.attr('allow') == 'True') {
                            $('.authority[authority=' + $this.attr('authority') + ']', '.metro_option_group').attr('checked', $this.attr('allow'));
                        }
                    });
                }

                hideLoading();
            }
        });
    }
</script>
<data id="authorityList">
    @if (Model.AuthorityList != null)
    {
    <list>
        @foreach (var item in Model.AuthorityList)
        {
        <item id='@item.ID' authority='@item.Authority' allow='@item.Allow'></item>
        }
    </list>
    }
</data>
